
Ext.isFunction = function(f) {
    return ( f && typeof(f) == 'function' );
}

Mappl.Utils = {};

Mappl.Utils.Array = function() {
    this.array                  = null ;

    var result = "this.array = new Array(" ;
    if ( arguments.length > 0 ) {
        for ( var i = 0, l = arguments.length ; i < l ; i ++ ) {
            if ( i > 0 ) result += ", " ;
            if ( typeof arguments[i] == "string" ) {
                result += "'" + arguments[i] + "'" ;
            } else {
                result += arguments[i] ;
            }
        }
    }
    
    result += ");"
    eval( result );

    this.setOwnsElements(true);

    return this ;
}

Mappl.Utils.Array.prototype = {
    setOwnsElements : function( aState ) {
        this.flOwnsElements       = aState;
        this.array.flOwnsElements = aState;
    },
    getDDXMLClass : function() {
        return "Mappl.Utils.Array";
    },
    fromDDXMLDoc : function( aNode ) {
        this.array = Array.fromDDXMLDoc( aNode );
        
        if ( this.flOwnsElements ) {
            this.array.setOwner( this );
        }
        
        /*if ( this.flOwnsElements ) {
            //this.array.setOwner( this );
            for ( var i = 0, l = getLength(); i < l; i++ ) {
                var item = this.get(i);
                if ( objectXML.TOwnedObject.prototype.hasSetOwner( item ) ) { 
                    item.setOwner( this ) ;
                }
            }
        }*/
    },
    toDDXML : function( aName ) {
        if ( !this.array || this.array == null || this.array.length == 0 ) {
            return;
        }
        
        if ( Mappl.XML.TOwnedObject.hasIsChanged( this.array ) && !this.array.isChanged( true ) ) {
            return;
        }
        
        var items  = this.array.itemsToDDXML();
        var Result = Object.getDDXMLTag( aName, '', items );
        return Result;
    },
    toArray : function() {
        return this.clone().array ;
    },
    get : function(index) {
        return this.array[index] ;
    },
    set : function(index, value) {
        this.array[index] = value ;
        if ( this.flOwnsElements && Mappl.XML.TOwnedObject.hasSetOwner( value ) ) { 
            value.setOwner( this ) ; 
        }
    },
    insert : function(index, value) {
        for (var i = this.array.length ; i > index ; i --) {
            this.array[i] = this.array[i - 1] ;
        }
        this.set( index, value );
    },
    getLength : function() {
        return this.array.length ;
    },
    setLength : function(length) {
        this.array.length = length ;
    },
    add : function(item) {
        this.set( this.getLength(), item );
    },
    addElement : function(item) {
        this.set(this.getLength(), item);
    },
    addAll : function(items) {
        for (var i = 0, l = items.getLength ? items.getLength() : items.length ; i < l ; i ++) {
            if ( items.get ) { this.addElement( items.get(i) ); } 
            else { this.addElement( items[i] ); }
        }
        return this ;
    },
    removeElement : function(item) {
        var flag = 0 ;
        for (var i = 0 ; i < this.getLength(); i++)
            if (flag) this.set(i-1, this.get(i)); else if (this.get(i) == item) flag = true ;
        if (flag) this.setLength(this.getLength() - 1);
    },
    removeElementAt : function(index) {
        if (index < this.getLength()) {
            for (var i = index+1 ; i < this.getLength(); i++) this.set(i-1, this.get(i));
            this.setLength(this.getLength() - 1);
        }
    },
    firstElement : function() {
        if (this.getLength() > 0) return this.get(0);
        return null ;
    },
    lastElement : function() {
        if (this.getLength() > 0) return this.get(this.getLength() - 1);
        return null ;
    },
    containsElement : function(item) {
        for (var i = 0 ; i < this.getLength(); i++) if (this.get(i) == item) return true ;
        return false ;
    },
    indexOf : function(item) {
        for (var i = 0, l = this.getLength(); i < l ; i ++)
            if (this.get(i) == item) return i ;
        return null ;
    },
    clone : function() {
        var c = new Mappl.Utils.Array();
        for (var i = 0 ; i < this.getLength(); i++) c.addElement(this.get(i));
        return c ;
    },
    pop : function() {
        var Result = this.lastElement();
        if ( Result != null) this.setLength( this.getLength() - 1 );
        return Result ;
    },
    join : function( separator ) {
        return this.array.join(separator);
    },
    isEmpty : function() {
        return this.array.length == 0 ;
    }

};

Mappl.Utils.Array.prototype.push = Mappl.Utils.Array.prototype.addElement;
Mappl.Utils.Array.prototype.peek = Mappl.Utils.Array.prototype.lastElement;
    
//Object.extend( ddTools.Array.prototype, objectXML.TOwnedObject.prototype );
Ext.apply( Mappl.Utils.Array.prototype, Mappl.XML.TOwnedObject );

/*
Ext.apply( Array.prototype, {
    isChanged : function( aClearState ) {
        var count = 0 ;
        for (var i = 0, l = this.length ; i < l ; i ++) {
            var item = this[i] ;
            if (Mappl.XML.TOwnedObject.hasIsChanged(item) && item.isChanged(aClearState)) {
                count ++ ;
            }
        }
        return count > 0 ;
    }
});
*/

Ext.applyIf( Mappl.Utils.Array.prototype, {
    isChanged : function( aClearState ) {
        var Result = this.array.isChanged( aClearState );
        if ( aClearState ) {
            this.changedCount = 0; 
        }
        return Result;
    }
} );

/*******************************************************************************
* Mappl.Point
*******************************************************************************/
Mappl.Point = function ( aX, aY ) {
    this.x = aX;
    this.y = aY;
}

if ( ! ( "console" in window ) || ! ( "firebug" in console ) )
{
    window.console = {
        log : function( aMsg ) {
            //alert(aMsg);
        }
        , dir : function( aException ) {
        }
    }
}

function listObjectProperties(aObject, aIntoEl, aPrefix) {
    var prefix = typeof aPrefix == "undefined" ? "_" : aPrefix + "_" ;
    if (typeof aIntoEl == "undefined") {
        var w = window.open("", "", "");
        with (w.document) {
            open();
            write("<ul>");
            var ix = 1 ;
            for (var name in aObject) {
                write("<li>name [" + name + "] value [<a href='javascript:void(0)'>" + aObject[name] + "</a>]<ul sytle='display : none' id='" + prefix + ix + "'></ul></li>");
            }
            write("</ul>");
            close();
        }
    } else {

    }
}